/*   1:    */ package org.intellij.plugins.relaxNG.validation;
/*   2:    */ 
/*   3:    */

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.AtomicNotNullLazyValue;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.PsiElementProcessor.CollectElements;
import com.intellij.psi.util.CachedValue;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValueProvider.Result;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.xml.XmlFile;
import com.intellij.xml.util.XmlUtil;
import com.thaiopensource.datatype.xsd.DatatypeLibraryFactoryImpl;
import com.thaiopensource.relaxng.impl.SchemaReaderImpl;
import com.thaiopensource.util.PropertyMap;
import com.thaiopensource.util.PropertyMapBuilder;
import com.thaiopensource.validate.Schema;
import com.thaiopensource.xml.sax.Sax2XMLReaderCreator;
import com.thaiopensource.xml.sax.XMLReaderCreator;
import org.intellij.plugins.relaxNG.compact.RncFileType;
import org.intellij.plugins.relaxNG.model.resolve.RelaxIncludeIndex;
import org.jetbrains.annotations.NotNull;
import org.kohsuke.rngom.ast.builder.BuildException;
import org.kohsuke.rngom.ast.builder.IncludedGrammar;
import org.kohsuke.rngom.ast.builder.SchemaBuilder;
import org.kohsuke.rngom.ast.om.ParsedPattern;
import org.kohsuke.rngom.binary.SchemaBuilderImpl;
import org.kohsuke.rngom.binary.SchemaPatternBuilder;
import org.kohsuke.rngom.digested.DPattern;
import org.kohsuke.rngom.digested.DSchemaBuilderImpl;
import org.kohsuke.rngom.dt.CachedDatatypeLibraryFactory;
import org.kohsuke.rngom.dt.CascadingDatatypeLibraryFactory;
import org.kohsuke.rngom.dt.DoNothingDatatypeLibraryFactoryImpl;
import org.kohsuke.rngom.dt.builtin.BuiltinDatatypeLibraryFactory;
import org.kohsuke.rngom.parse.IllegalSchemaException;
import org.relaxng.datatype.DatatypeLibraryFactory;
import org.relaxng.datatype.helpers.DatatypeLibraryLoader;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import java.io.StringReader;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */
/*  33:    */
/*  34:    */
/*  35:    */
/*  36:    */
/*  37:    */
/*  38:    */
/*  39:    */
/*  40:    */
/*  41:    */
/*  42:    */
/*  43:    */
/*  44:    */
/*  45:    */
/*  46:    */
/*  47:    */
/*  48:    */
/*  49:    */
/*  50:    */
/*  51:    */
/*  52:    */
/*  53:    */ 
/*  54:    */ public class RngParser
/*  55:    */ {
/*  56: 78 */   private static final Logger a = Logger.getInstance("#org.intellij.plugins.relaxNG.validation.RngParser");
/*  57: 80 */   private static final NotNullLazyValue<DatatypeLibraryFactory> b = new AtomicNotNullLazyValue()
/*  58:    */   {
/*  59:    */     @NotNull
/*  60:    */     protected DatatypeLibraryFactory compute()
/*  61:    */     {
/*  62: 84 */       void tmp32_29 = new BuiltinDatatypeLibraryFactory(new CachedDatatypeLibraryFactory(new CascadingDatatypeLibraryFactory(RngParser.access$000(), new DatatypeLibraryLoader())));
/*  63: 84 */       if (tmp32_29 == null) {
/*  64: 84 */         throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/validation/RngParser$1", "compute" }));
/*  65:    */       }
/*  66: 84 */       return tmp32_29;
/*  67:    */     }
/*  68:    */   };
/*  69:    */   
/*  70:    */   private static DatatypeLibraryFactory a()
/*  71:    */   {
/*  72:    */     try
/*  73:    */     {
/*  74: 96 */       return new DatatypeLibraryFactoryImpl();
/*  75:    */     }
/*  76:    */     catch (Throwable localThrowable)
/*  77:    */     {
/*  78: 98 */       a.error("Could not create DT library implementation 'com.thaiopensource.datatype.xsd.DatatypeLibraryFactoryImpl'. Plugin's classpath seems to be broken.", localThrowable);
/*  79:    */     }
/*  80: 99 */     return new DoNothingDatatypeLibraryFactoryImpl();
/*  81:    */   }
/*  82:    */   
/*  83:103 */   static final Key<CachedValue<Schema>> SCHEMA_KEY = Key.create("SCHEMA");
/*  84:104 */   static final Key<CachedValue<DPattern>> PATTERN_KEY = Key.create("PATTERN");
/*  85:106 */   public static final DefaultHandler DEFAULT_HANDLER = new DefaultHandler()
/*  86:    */   {
/*  87:    */     public void error(SAXParseException paramAnonymousSAXParseException)
/*  88:    */       throws SAXException
/*  89:    */     {
/*  90:108 */       RngParser.a.info("e.getMessage() = " + paramAnonymousSAXParseException.getMessage() + " [" + paramAnonymousSAXParseException.getSystemId() + "]");
/*  91:109 */       RngParser.a.info(paramAnonymousSAXParseException);
/*  92:    */     }
/*  93:    */   };
/*  94:113 */   static final PropertyMap EMPTY_PROPS = new PropertyMapBuilder().toPropertyMap();
/*  95:    */   
/*  96:    */   public static DPattern getCachedPattern(PsiFile paramPsiFile, final ErrorHandler paramErrorHandler)
/*  97:    */   {
/*  98:116 */     CachedValuesManager localCachedValuesManager = CachedValuesManager.getManager(paramPsiFile.getProject());
/*  99:    */     
/* 100:118 */     (DPattern)localCachedValuesManager.getCachedValue(paramPsiFile, PATTERN_KEY, new CachedValueProvider()
/* 101:    */     {
/* 102:    */       public CachedValueProvider.Result<DPattern> compute()
/* 103:    */       {
/* 104:120 */         return CachedValueProvider.Result.create(RngParser.parsePattern(this.val$descriptorFile, paramErrorHandler, false), new Object[] { this.val$descriptorFile });
/* 105:    */       }
/* 106:120 */     }, false);
/* 107:    */   }
/* 108:    */   
/* 109:    */   public static DPattern parsePattern(PsiFile paramPsiFile, ErrorHandler paramErrorHandler, boolean paramBoolean)
/* 110:    */   {
/* 111:    */     try
/* 112:    */     {
/* 113:127 */       org.kohsuke.rngom.parse.Parseable localParseable = a(paramPsiFile, paramErrorHandler);
/* 114:128 */       if (paramBoolean) {
/* 115:129 */         localParseable.parse(new SchemaBuilderImpl(paramErrorHandler, (DatatypeLibraryFactory)b.getValue(), new SchemaPatternBuilder()));
/* 116:    */       } else {
/* 117:131 */         return (DPattern)localParseable.parse(new DSchemaBuilderImpl());
/* 118:    */       }
/* 119:    */     }
/* 120:    */     catch (BuildException localBuildException)
/* 121:    */     {
/* 122:134 */       a.info(localBuildException);
/* 123:    */     }
/* 124:    */     catch (IllegalSchemaException localIllegalSchemaException)
/* 125:    */     {
/* 126:136 */       VirtualFile localVirtualFile = paramPsiFile.getVirtualFile();
/* 127:137 */       if (localVirtualFile != null) {
/* 128:138 */         if (a.isDebugEnabled()) {
/* 129:139 */           a.debug("invalid schema: " + localVirtualFile.getPresentableUrl(), localIllegalSchemaException);
/* 130:    */         } else {
/* 131:141 */           a.info("invalid schema: " + localVirtualFile.getPresentableUrl() + ". [" + localIllegalSchemaException.getMessage() + "]");
/* 132:    */         }
/* 133:    */       }
/* 134:    */     }
/* 135:145 */     return null;
/* 136:    */   }
/* 137:    */   
/* 138:    */   private static org.kohsuke.rngom.parse.Parseable a(final PsiFile paramPsiFile, ErrorHandler paramErrorHandler)
/* 139:    */   {
/* 140:149 */     InputSource localInputSource = a(paramPsiFile);
/* 141:151 */     if (paramPsiFile.getFileType() == RncFileType.getInstance()) {
/* 142:152 */       new org.kohsuke.rngom.parse.compact.CompactParseable(localInputSource, paramErrorHandler)
/* 143:    */       {
/* 144:    */         public ParsedPattern parseInclude(String paramAnonymousString1, SchemaBuilder paramAnonymousSchemaBuilder, IncludedGrammar paramAnonymousIncludedGrammar, String paramAnonymousString2)
/* 145:    */           throws BuildException, IllegalSchemaException
/* 146:    */         {
/* 147:156 */           return super.parseInclude(RngParser.resolveURI(paramPsiFile, paramAnonymousString1), paramAnonymousSchemaBuilder, paramAnonymousIncludedGrammar, paramAnonymousString2);
/* 148:    */         }
/* 149:    */       };
/* 150:    */     }
/* 151:160 */     new org.kohsuke.rngom.parse.xml.SAXParseable(localInputSource, paramErrorHandler)
/* 152:    */     {
/* 153:    */       public ParsedPattern parseInclude(String paramAnonymousString1, SchemaBuilder paramAnonymousSchemaBuilder, IncludedGrammar paramAnonymousIncludedGrammar, String paramAnonymousString2)
/* 154:    */         throws BuildException, IllegalSchemaException
/* 155:    */       {
/* 156:164 */         return super.parseInclude(RngParser.resolveURI(paramPsiFile, paramAnonymousString1), paramAnonymousSchemaBuilder, paramAnonymousIncludedGrammar, paramAnonymousString2);
/* 157:    */       }
/* 158:    */     };
/* 159:    */   }
/* 160:    */   
/* 161:    */   public static String resolveURI(PsiFile paramPsiFile, String paramString)
/* 162:    */   {
/* 163:171 */     XmlFile localXmlFile = XmlUtil.findXmlFile(paramPsiFile, paramString);
/* 164:173 */     if (localXmlFile != null)
/* 165:    */     {
/* 166:174 */       VirtualFile localVirtualFile = localXmlFile.getVirtualFile();
/* 167:175 */       if (localVirtualFile != null)
/* 168:    */       {
/* 169:176 */         PsiDocumentManager localPsiDocumentManager = PsiDocumentManager.getInstance(localXmlFile.getProject());
/* 170:177 */         Document localDocument = localPsiDocumentManager.getCachedDocument(localXmlFile);
/* 171:178 */         if (localDocument != null) {}
/* 172:183 */         paramString = reallyFixIDEAUrl(localVirtualFile.getUrl());
/* 173:    */       }
/* 174:    */     }
/* 175:186 */     return paramString;
/* 176:    */   }
/* 177:    */   
/* 178:    */   public static String reallyFixIDEAUrl(String paramString)
/* 179:    */   {
/* 180:190 */     String str = VfsUtil.fixIDEAUrl(paramString);
/* 181:191 */     if (!SystemInfo.isWindows)
/* 182:    */     {
/* 183:195 */       str = str.replaceFirst("file:/+", "file:/");
/* 184:196 */       if (a.isDebugEnabled()) {
/* 185:197 */         a.debug("Fixed URL: " + paramString + " -> " + str);
/* 186:    */       }
/* 187:    */     }
/* 188:200 */     return str;
/* 189:    */   }
/* 190:    */   
/* 191:    */   public static Schema getCachedSchema(XmlFile paramXmlFile)
/* 192:    */   {
/* 193:204 */     CachedValue localCachedValue = (CachedValue)paramXmlFile.getUserData(SCHEMA_KEY);
/* 194:205 */     if (localCachedValue == null)
/* 195:    */     {
/* 196:206 */       CachedValueProvider local6 = new CachedValueProvider()
/* 197:    */       {
/* 198:    */         public CachedValueProvider.Result<Schema> compute()
/* 199:    */         {
/* 200:208 */           InputSource localInputSource = RngParser.a(this.val$descriptorFile);
/* 201:    */           try
/* 202:    */           {
/* 203:211 */             Schema localSchema = new RngParser.MySchemaReader(this.val$descriptorFile).createSchema(localInputSource, RngParser.EMPTY_PROPS);
/* 204:212 */             PsiElementProcessor.CollectElements localCollectElements = new PsiElementProcessor.CollectElements();
/* 205:213 */             RelaxIncludeIndex.processForwardDependencies(this.val$descriptorFile, localCollectElements);
/* 206:214 */             if (localCollectElements.getCollection().size() > 0) {
/* 207:215 */               return CachedValueProvider.Result.create(localSchema, new Object[] { localCollectElements.toArray(), this.val$descriptorFile });
/* 208:    */             }
/* 209:217 */             return CachedValueProvider.Result.createSingleDependency(localSchema, this.val$descriptorFile);
/* 210:    */           }
/* 211:    */           catch (Exception localException)
/* 212:    */           {
/* 213:220 */             RngParser.a.info(localException);
/* 214:    */           }
/* 215:221 */           return CachedValueProvider.Result.createSingleDependency(null, this.val$descriptorFile);
/* 216:    */         }
/* 217:225 */       };
/* 218:226 */       CachedValuesManager localCachedValuesManager = CachedValuesManager.getManager(paramXmlFile.getProject());
/* 219:227 */       localCachedValue = localCachedValuesManager.createCachedValue(local6, false);
/* 220:228 */       paramXmlFile.putUserData(SCHEMA_KEY, localCachedValue);
/* 221:    */     }
/* 222:230 */     return (Schema)localCachedValue.getValue();
/* 223:    */   }
/* 224:    */   
/* 225:    */   private static InputSource a(PsiFile paramPsiFile)
/* 226:    */   {
/* 227:234 */     InputSource localInputSource = new InputSource(new StringReader(paramPsiFile.getText()));
/* 228:235 */     VirtualFile localVirtualFile = paramPsiFile.getVirtualFile();
/* 229:236 */     if (localVirtualFile != null) {
/* 230:237 */       localInputSource.setSystemId(reallyFixIDEAUrl(localVirtualFile.getUrl()));
/* 231:    */     }
/* 232:239 */     return localInputSource;
/* 233:    */   }
/* 234:    */   
/* 235:    */   static class MySchemaReader
/* 236:    */     extends SchemaReaderImpl
/* 237:    */   {
/* 238:    */     private final PsiFile a;
/* 239:    */     
/* 240:    */     public MySchemaReader(PsiFile paramPsiFile)
/* 241:    */     {
/* 242:246 */       this.a = paramPsiFile;
/* 243:    */     }
/* 244:    */     
/* 245:    */     protected com.thaiopensource.relaxng.parse.Parseable createParseable(XMLReaderCreator paramXMLReaderCreator, InputSource paramInputSource, ErrorHandler paramErrorHandler)
/* 246:    */     {
/* 247:250 */       if (this.a.getFileType() == RncFileType.getInstance()) {
/* 248:251 */         return new com.thaiopensource.relaxng.parse.compact.CompactParseable(paramInputSource, paramErrorHandler);
/* 249:    */       }
/* 250:253 */       return new com.thaiopensource.relaxng.parse.sax.SAXParseable(new Sax2XMLReaderCreator(), paramInputSource, paramErrorHandler);
/* 251:    */     }
/* 252:    */   }
/* 253:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.plugins.relaxNG.validation.RngParser

 * JD-Core Version:    0.7.0.1

 */